package com.wx.thread;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @description: Volatile测试
 * @author: wangxun
 * @createDate: 2021/3/18
 * @version: 1.0
 */
public class VolatileTest {
    private boolean flag = true;


    public void runTask(){
        System.out.println("run");
        int i = 0;
        while (flag){

            Person.id= new Random().nextInt(100);
        }
        System.out.println("end");
    }

    public static void main(String[] args) throws InterruptedException {
        final VolatileTest volatileTest = new VolatileTest();

        final Thread aVolatile = new Thread(volatileTest::runTask, "volatile");
        aVolatile.start();
        TimeUnit.SECONDS.sleep(1);
        volatileTest.flag=false;
    }
}
